<script setup lang="ts">
import {User, Lock} from '@element-plus/icons-vue'
import {reactive, ref} from 'vue'
import useUserStore from "@/store/modules/user";
import router from "@/router";
import {useRoute} from 'vue-router'
import {ElNotification, FormRules} from "element-plus";
import {getTimePeriod} from "@/utils/times";

let loading = ref(false);
let userStore = useUserStore();
let loginForm = reactive({
  username: 'admin',
  password: '111111'
})
const validateUsername = (rule: any, value: any, callback: any) => {
  if (value === '') {
    callback(new Error('请输入用户名'));
  } else if (value.length < 5) {
    callback(new Error('用户名至少为5位'));
  } else {
    callback();
  }
}
const validatePassword = (rule: any, value: any, callback: any) => {
  if (value === '') {
    callback(new Error('请输入密码'));
  } else if (value.length < 6) {
    callback(new Error('密码至少为6位'));
  } else {
    callback();
  }
}
const loginFormRef = ref()
const rules = reactive<FormRules>({
  username: [
    // {required: true, message: '用户名不能为空', trigger: 'change'},
    // {min: 5, message: '用户名至少为5位', trigger: 'change'},
    {trigger: 'change', validator: validateUsername}
  ],
  password: [
    // {required: true, message: '密码不能为空', trigger: 'change'},
    // {min: 6, message: '密码至少为6位', trigger: 'change'},
    {trigger:'change',validator:validatePassword}
  ],
})

// 从路由参数中获取redirect
let route = useRoute();
let redirect = route.query.redirect;

const login = async () => {
  // 表单校验 校验通过后才会执行登录逻辑
  await loginFormRef.value.validate();
  loading.value = true;
  // 登录逻辑
  try {
    await userStore.userLogin(loginForm)
    // 登录成功后跳转到首页或者重定向
    router.push(redirect ? redirect as string : '/')
    loading.value = false;
    ElNotification(
        {
          title: `Hi,${getTimePeriod()}好！`,
          message: '欢迎回来',
          type: 'success'
        }
    )
  } catch (error) {
    loading.value = false;
    ElNotification(
        {
          title: '登录失败',
          message: error.message,
          type: 'error'
        }
    )
  }
}
</script>

<template>
  <div class="login_container">
    <el-row>
      <el-col :span="12" :xs="0"></el-col>
      <el-col :span="12" :xs="24">
        <el-form class="login_form" :model="loginForm" :rules="rules" ref="loginFormRef">
          <h1>Hello</h1>
          <h2>欢迎来到硅谷甄选</h2>
          <el-form-item prop="username">
            <el-input :prefix-icon="User" placeholder="请输入用户名" v-model="loginForm.username"/>
          </el-form-item>
          <el-form-item prop="password">
            <el-input :prefix-icon="Lock" type="password" show-password placeholder="请输入密码"
                      v-model="loginForm.password"/>
          </el-form-item>
          <el-form-item>
            <el-button class="login_btn" type="primary" size="default" :loading="loading" @click="login">登录
            </el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<style scoped>
.login_container {
  width: 100%;
  height: 100vh;
  background: url('@/assets/images/background.jpg') no-repeat;
  background-size: cover;

  .login_form {
    position: relative;
    width: 80%;
    top: 30vh;
    background: url('@/assets/images/login_form.png') no-repeat;
    background-size: cover;
    padding: 40px;

    h1 {
      color: white;
      font-size: 40px;
    }

    h2 {
      color: white;
      font-size: 20px;
      margin: 20px 0;
    }

    .login_btn {
      width: 100%;
    }
  }
}


</style>